﻿namespace MultipleMonitorVideo.ViewModels
{
    using System;
    using System.Windows.Input;
    using Microsoft.Win32;

    public class VideoControlViewModel : ModuleControlViewModel
    {
        private VideoViewModel videoViewModel;
        private string fileName;
        private bool isVideoLoaded;
        private bool isVideoPlaying;
        
        public VideoControlViewModel()
        {
            this.SelectFileCommand = new ActionCommand(this.SelectFile, () => true);
            this.PlayNewVideoCommand = new ActionCommand(this.PlayNewVideo, () => true);
            this.PlayVideoCommand = new ActionCommand(this.PlayVideo, () => this.isVideoLoaded && !this.isVideoPlaying);
            this.PauseVideoCommand = new ActionCommand(this.PauseVideo, () => this.isVideoLoaded && this.isVideoPlaying);
            this.StopVideoCommand = new ActionCommand(this.StopVideo, () => this.isVideoLoaded);
        }

        public string FileName
        {
            get
            {
                return this.fileName;
            }

            set
            {
                if (this.fileName != value)
                {
                    this.fileName = value;
                    this.NotifyOfPropertyChange();
                }
            }
        }

        public bool IsVideoLoaded
        {
            get
            {
                return this.isVideoLoaded;
            }

            set
            {
                if (this.isVideoLoaded != value)
                {
                    this.isVideoLoaded = value;
                    this.NotifyOfPropertyChange();
                }
            }
        }

        public bool IsVideoPlaying
        {
            get
            {
                return this.isVideoPlaying;
            }

            set
            {
                if (this.isVideoPlaying != value)
                {
                    this.isVideoPlaying = value;
                    this.NotifyOfPropertyChange();
                }
            }
        }

        public ICommand SelectFileCommand
        {
            get;
            private set;
        }

        public ICommand PlayNewVideoCommand
        {
            get;
            private set;
        }

        public ICommand PlayVideoCommand
        {
            get;
            private set;
        }

        public ICommand PauseVideoCommand
        {
            get;
            private set;
        }

        public ICommand StopVideoCommand
        {
            get;
            private set;
        }

        public override void SetViewModel(ViewModel viewModel)
        {
            this.SetViewModel(viewModel as VideoViewModel);
        }

        public void SetViewModel(VideoViewModel viewModel)
        {
            this.videoViewModel = viewModel;
        }

        private void SelectFile()
        {
            var openFileDialog = new OpenFileDialog
            {
                Filter = "Video Files|*.avi;*.flv;*.mkv;*.mov;*.mpeg;*.mpg;*.mp4;*.ogg;*.webm;*.wmv|All Files|*.*"
            };

            bool? userClickedOk = openFileDialog.ShowDialog();

            if (userClickedOk == true)
            {
                string fileName = openFileDialog.FileName;
                this.FileName = fileName;
            }
        }

        private void PlayNewVideo()
        {
            if (this.videoViewModel != null)
            {
                if (string.IsNullOrEmpty(this.FileName))
                {
                    return;
                }

                this.videoViewModel.FileName = new Uri(this.FileName);
                this.videoViewModel.PlayVideo();
                this.isVideoLoaded = true;
                this.isVideoPlaying = true;
            }
        }

        private void PlayVideo()
        {
            if (this.videoViewModel != null)
            {
                this.videoViewModel.PlayVideo();
                this.isVideoPlaying = true;
            }
        }

        private void PauseVideo()
        {
            if (this.videoViewModel != null)
            {
                this.videoViewModel.PauseVideo();
                this.isVideoPlaying = false;
            }
        }

        private void StopVideo()
        {
            if (this.videoViewModel != null)
            {
                this.videoViewModel.StopVideo();
                this.isVideoPlaying = false;
            }
        }
    }
}
